package com.shaoshaohuo.app.ui.shipper;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.NaviPara;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.PoiEntity;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.MyBaseAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.ViewHelper;
import com.shaoshaohuo.app.utils.map.AMapUtil;
import com.shaoshaohuo.app.utils.map.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * AMapV1地图中简单介绍poisearch搜索
 */
public class PoiKeywordSearchActivity extends BaseActivity implements OnMarkerClickListener, TextWatcher, OnPoiSearchListener,
		OnClickListener {

	private AMap aMap;
	private EditText searchText;// 输入搜索关键字
	private String keyWord = "";// 要输入的poi搜索关键字
	private ProgressDialog progDialog = null;// 搜索时进度条
	// private EditText editCity;// 要输入的城市名字或者城市区号
	private PoiResult poiResult; // poi返回的结果
	private int currentPage = 0;// 当前页面，从0开始计数
	private PoiSearch.Query query;// Poi查询条件类
	private PoiSearch poiSearch;// POI搜索

	private ListView listView;
	private TopbarView mTopbarView;

	private int mSelectPosition = 0;
	private MyAdapter<PoiItem> mAdapter;
	private ArrayList<PoiItem> poiItems;
	private View searButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.poikeywordsearch_activity);
		init();
	}

	/**
	 * 初始化AMap对象
	 */
	private void init() {

		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mTopbarView.setCenterText("搜索地址");
		mTopbarView.setLeftView(true, true);
		mTopbarView.setRightText("确定");
		mTopbarView.setRightClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (poiItems == null || poiItems.isEmpty()) {
					showToast("请选择地址");
				} else {
					PoiItem poiItem = poiItems.get(mSelectPosition);
					PoiEntity poi = new PoiEntity();
					poi.setAddress(poiItem.getSnippet());
					poi.setId(poiItem.getCityCode());
					poi.setCityname(poiItem.getCityName());
					poi.setLat(poiItem.getLatLonPoint().getLatitude());
					poi.setLng(poiItem.getLatLonPoint().getLongitude());
					poi.setTitle(poiItem.getTitle());

					Intent intent = new Intent();
					intent.putExtra(ExtraName.poi, poi);
					setResult(RESULT_OK, intent);
					finish();
				}

			}
		});

		// if (aMap == null) {
		// aMap = ((SupportMapFragment) getSupportFragmentManager()
		// .findFragmentById(R.id.map)).getMap();
		setUpMap();
		// }
		listView = (ListView) this.findViewById(R.id.listview);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				mSelectPosition = arg2;
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	/**
	 * 设置页面监听
	 */
	private void setUpMap() {
		searButton = findViewById(R.id.searchButton);
		searButton.setOnClickListener(this);
		// Button nextButton = (Button) findViewById(R.id.nextButton);
		// nextButton.setOnClickListener(this);
		searchText = (EditText) findViewById(R.id.keyWord);
		searchText.addTextChangedListener(this);// 添加文本输入框监听事件
		// editCity = (EditText) findViewById(R.id.city);
		// aMap.setOnMarkerClickListener(this);// 添加点击marker监听事件
		// aMap.setInfoWindowAdapter(this);// 添加显示infowindow监听事件

		searchText.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

					onClick(searButton);

					return true;
				}
				return false;
			}

		});

	}

	/**
	 * 点击搜索按钮
	 */
	public void searchButton() {
		keyWord = AMapUtil.checkEditText(searchText);
		if ("".equals(keyWord)) {
			ToastUtil.show(PoiKeywordSearchActivity.this, "请输入搜索关键字");
			return;
		} else {
			doSearchQuery();

//			DeviceHelper.hideIME(searchText);
		}
	}

	/**
	 * 点击下一页按钮
	 */
	public void nextButton() {
		if (query != null && poiSearch != null && poiResult != null) {
			if (poiResult.getPageCount() - 1 > currentPage) {
				currentPage++;
				query.setPageNum(currentPage);// 设置查后一页
				poiSearch.searchPOIAsyn();
			} else {
				ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
			}
		}
	}

	/**
	 * 显示进度框
	 */
	private void showProgressDialog() {
		if (progDialog == null)
			progDialog = new ProgressDialog(this);
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(false);
		progDialog.setMessage("正在搜索:\n" + keyWord);
		progDialog.show();
	}

	/**
	 * 隐藏进度框
	 */
	private void dissmissProgressDialog() {
		if (progDialog != null) {
			progDialog.dismiss();
		}
	}

	/**
	 * 开始进行poi搜索
	 */
	protected void doSearchQuery() {
//		showProgressDialog();// 显示进度框
		currentPage = 0;
		// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
		query = new PoiSearch.Query(keyWord,"", "");
		query.setPageSize(50);// 设置每页最多返回多少条poiitem
		query.setPageNum(currentPage);// 设置查第一页

		poiSearch = new PoiSearch(this, query);
		poiSearch.setOnPoiSearchListener(this);
		poiSearch.searchPOIAsyn();
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		marker.showInfoWindow();
		return false;
	}

	// @Override
	// public View getInfoContents(Marker marker) {
	// return null;
	// }

	// @Override
	// public View getInfoWindow(final Marker marker) {
	// View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri,
	// null);
	// TextView title = (TextView) view.findViewById(R.id.title);
	// title.setText(marker.getTitle());
	//
	// TextView snippet = (TextView) view.findViewById(R.id.snippet);
	// snippet.setText(marker.getSnippet());
	// ImageButton button = (ImageButton) view
	// .findViewById(R.id.start_amap_app);
	// // 调起高德地图app
	// button.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// startAMapNavi(marker);
	// }
	// });
	// return view;
	// }

	/**
	 * 调起高德地图导航功能，如果没安装高德地图，会进入异常，可以在异常中处理，调起高德地图app的下载页面
	 */
	public void startAMapNavi(Marker marker) {
		// 构造导航参数
		NaviPara naviPara = new NaviPara();
		// 设置终点位置
		naviPara.setTargetPoint(marker.getPosition());
		// 设置导航策略，这里是避免拥堵
		naviPara.setNaviStyle(NaviPara.DRIVING_AVOID_CONGESTION);

		// 调起高德地图导航
		try {
			AMapUtils.openAMapNavi(naviPara, getApplicationContext());
		} catch (com.amap.api.maps2d.AMapException e) {

			// 如果没安装会进入异常，调起下载页面
			AMapUtils.getLatestAMapApp(getApplicationContext());

		}

	}

	/**
	 * 判断高德地图app是否已经安装
	 */
	public boolean getAppIn() {
		PackageInfo packageInfo = null;
		try {
			packageInfo = this.getPackageManager().getPackageInfo("com.autonavi.minimap", 0);
		} catch (NameNotFoundException e) {
			packageInfo = null;
			e.printStackTrace();
		}
		// 本手机没有安装高德地图app
		if (packageInfo != null) {
			return true;
		}
		// 本手机成功安装有高德地图app
		else {
			return false;
		}
	}

	/**
	 * 获取当前app的应用名字
	 */
	public String getApplicationName() {
		PackageManager packageManager = null;
		ApplicationInfo applicationInfo = null;
		try {
			packageManager = getApplicationContext().getPackageManager();
			applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			applicationInfo = null;
		}
		String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
		return applicationName;
	}

	/**
	 * poi没有搜索到数据，返回一些推荐城市的信息
	 */
	private void showSuggestCity(List<SuggestionCity> cities) {
		String infomation = "推荐城市\n";
		for (int i = 0; i < cities.size(); i++) {
			infomation += "城市名称:" + cities.get(i).getCityName() + "城市区号:" + cities.get(i).getCityCode() + "城市编码:"
					+ cities.get(i).getAdCode() + "\n";
		}
		ToastUtil.show(PoiKeywordSearchActivity.this, infomation);

	}

	@Override
	public void afterTextChanged(Editable s) {
		if(s.length()>0){
			onClick(searButton);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// String newText = s.toString().trim();
		// Inputtips inputTips = new Inputtips(PoiKeywordSearchActivity.this,
		// new InputtipsListener() {
		//
		// @Override
		// public void onGetInputtips(List<Tip> tipList, int rCode) {
		// if (rCode == 0) {// 正确返回
		// List<String> listString = new ArrayList<String>();
		// for (int i = 0; i < tipList.size(); i++) {
		// listString.add(tipList.get(i).getName());
		// }
		// ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
		// getApplicationContext(),
		// R.layout.route_inputs, listString);
		// searchText.setAdapter(aAdapter);
		// aAdapter.notifyDataSetChanged();
		// }
		// }
		// });
		// try {
		// inputTips.requestInputtips(newText, editCity.getText().toString());//
		// 第一个参数表示提示关键字，第二个参数默认代表全国，也可以为城市区号
		//
		// } catch (AMapException e) {
		// e.printStackTrace();
		// }
	}

	/**
	 * POI详情查询回调方法
	 */
	/**
	 * POI信息查询回调方法
	 */
	@Override
	public void onPoiSearched(PoiResult result, int rCode) {
		dissmissProgressDialog();// 隐藏对话框
		if (rCode == 0) {
			if (result != null && result.getQuery() != null) {// 搜索poi的结果
				if (result.getQuery().equals(query)) {// 是否是同一条
					poiResult = result;
					// 取得搜索到的poiitems有多少页
					poiItems = poiResult.getPois();// 取得第一页的poiitem数据，页数从数字0开始
					List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();// 当搜索不到poiitem数据时，会返回含有搜索关键字的城市信息

					if (poiItems != null && poiItems.size() > 0) {
						// aMap.clear();// 清理之前的图标
						// PoiOverlay poiOverlay = new PoiOverlay(aMap,
						// poiItems);
						// poiOverlay.removeFromMap();
						// poiOverlay.addToMap();
						// poiOverlay.zoomToSpan();

						if (mAdapter == null) {
							mAdapter = new MyAdapter<PoiItem>(this, poiItems, false);
							listView.setAdapter(mAdapter);
						} else {
							mSelectPosition = 0;
							mAdapter.setData(poiItems);
						}
						
						ViewHelper.setListVIewEmptyView(this, listView);

					} else if (suggestionCities != null && suggestionCities.size() > 0) {
						// showSuggestCity(suggestionCities);
//						ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
					} else {
//						ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
					}
				}
			} else {
//				ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
			}
		} else if (rCode == 27) {
			ToastUtil.show(PoiKeywordSearchActivity.this, R.string.error_network);
		} else if (rCode == 32) {
			ToastUtil.show(PoiKeywordSearchActivity.this, R.string.error_key);
		} else {
			ToastUtil.show(PoiKeywordSearchActivity.this, getString(R.string.error_other) + rCode);
		}

	}

	@Override
	public void onPoiItemSearched(PoiItem poiItem, int i) {

	}

	private class MyAdapter<T> extends MyBaseAdapter<PoiItem> {

		public MyAdapter(Context ctx, List<PoiItem> list, boolean flag) {
			super(ctx, list, flag);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = View.inflate(PoiKeywordSearchActivity.this, R.layout.view_poi_item, null);
			TextView name = (TextView) view.findViewById(R.id.textview_name);
			TextView address = (TextView) view.findViewById(R.id.textview_address);
			ImageView selectImage = (ImageView) view.findViewById(R.id.imageview_select);
			PoiItem poiItem = (PoiItem) list.get(position);

			// name.setText(poiItem.getTitle());
			// address.setText(poiItem.getSnippet());

			if (TextUtils.isEmpty(poiItem.getTitle())) {
				name.setVisibility(View.GONE);
			} else {
				name.setText(poiItem.getTitle());
			}

			if (TextUtils.isEmpty(poiItem.getSnippet())) {
				address.setVisibility(View.GONE);
			} else {
				address.setText(poiItem.getSnippet());
			}

			if (mSelectPosition == position) {
				selectImage.setVisibility(View.VISIBLE);
			} else {
				selectImage.setVisibility(View.GONE);
			}

			return view;
		}
	};

	/**
	 * Button点击事件回调方法
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		/**
		 * 点击搜索按钮
		 */
		case R.id.searchButton:
			searchButton();
			break;
		/**
		 * 点击下一页按钮
		 */
		// case R.id.nextButton:
		// nextButton();
		// break;
		default:
			break;
		}
	}
}
